home *** CD-ROM | disk | FTP | other *** search
- Path: news.NetVision.net.il!news
- From: "Poul A. Costinsky" <PoulACost@msn.com>
- Newsgroups: comp.lang.c++
- Subject: Re: overload + operator question
- Date: Mon, 18 Mar 1996 12:29:46 +0200
- Organization: NetVision LTD.
- Message-ID: <314D3B1A.11E4@msn.com>
- References: <1996Mar14.133124.12847@driftwood.cray.com>
- NNTP-Posting-Host: 194.90.116.11
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
- CC: vb@cray.com
-
- >> o3 = o2 + 100;
- >>// will be flagged as 'illegal' unless friend ob + ob defined:
- >> o3 = 100 + o2;The answer is in every serious book about C++ (did you read
- Stroustroup's ?).
- What you are actually doing is calling operator+ for 100, which
- is integer and therefore cannot accept parameter coord.
- If operator+ is a member function of class coord, then
- "this", which is the first parameter, must be of class coord.
- When operator+ is global function (not necessary friend) then
- constructor coord(int) can be applied and
- o3 = 100 + o2;
- becames
- o3 = coord(100) + o2;
-
- --
- Hope this helps.
- Regards,
- Poul.
- ("`-''-/").___..--''"`-._ ~~~~~~~~~~Poul A. Costinsky~~~~~~~~~~
- (`6_ 6 ) `-. ( ).`-.__.`) PoulACost@msn.com
- (_Y_.)' ._ ) `._ `. ``-..-'
- _..`--'_..-_/ /--'_.' ,'
- (il).-'' (li).' ((!.-
-